package edu.hawaii.ics414;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import edu.hawaii.ics414.Review;
import android.os.AsyncTask;


/**
 * 
 * @author Matt
 *
 */
public class GetReviews extends AsyncTask<String, Void, Review[]> {

  @Override
  /*
   * Required by the AsyncTask class. Sends data to the appropriate php page for updates/queries
   * (non-Javadoc)
   * 
   * @params params A variable amount of Strings to send as form data.
   * 
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  protected Review[] doInBackground(String... params) {
    String gameName = (params.length > 0) ? params[0] : "";

    Map<String, String> requestParams = new HashMap<String, String>();
    requestParams.put("gameName", gameName);
    String response =
        Ajax.send("http://the-game-rater.comuf.com/app/get_reviews.php", requestParams);
    if (response != null) {
      List<Review> reviews = new ArrayList<Review>();
      String[] rs = response.split(";");
      for (int i = 0; i < rs.length; i++) {
        String[] r = rs[i].split("`");
        if (r.length >= 4) {
          reviews.add(new Review(r[0], r[1], r[2], r[3]));
        }
      }
      Review[] temp = new Review[reviews.size()];
      return reviews.toArray(temp);
    }
    return null;
  }
}
